home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / Level 0 Macintosh 07Aug94 / Scrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  3.8 KB  |  125 lines  |  [TEXT/KAHL]

  1. /* Scrap.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #pragma options(pack_enums)
  25. #include <Scrap.h>
  26. #include <Memory.h>
  27. #include <Errors.h>
  28. #pragma options(!pack_enums)
  29.  
  30. #include "Scrap.h"
  31. #include "Memory.h"
  32.  
  33.  
  34. #define ScrapFlushThreshHold (4096L) /* max size that's kept in memory */
  35.  
  36.  
  37. EXECUTE(static MyBoolean                    ScrapInitialized = False;)
  38.  
  39.  
  40. /* initialize scrap handler by zeroing the scrap */
  41. MyBoolean        Eep_InitializeScrapHandler(void)
  42.     {
  43.         ERROR(ScrapInitialized,PRERR(ForceAbort,
  44.             "InitializeScrapHandler called multiple times"));
  45.         EXECUTE(ScrapInitialized = True;)
  46.         if (InfoScrap()->scrapState < 0)
  47.             {
  48.                 ZeroScrap(); /* initialize (probably not needed under multifinder) */
  49.             }
  50.         return True;
  51.     }
  52.  
  53.  
  54. /* shut down the scrap handler.  this doesn't do anything right now */
  55. void                Eep_ShutdownScrapHandler(void)
  56.     {
  57.         ERROR(!ScrapInitialized,PRERR(ForceAbort,"Scrap handler not initialized"));
  58.     }
  59.  
  60.  
  61. /* get a block containing a copy of the scrap */
  62. /* if a block couldn't be allocated, then it returns NIL */
  63. char*                GetCopyOfScrap(void)
  64.     {
  65.         char*                TheData;
  66.         long                Offset;
  67.         char**            TheHandle;
  68.         long                ScrapLength;
  69.  
  70.         ERROR(!ScrapInitialized,PRERR(ForceAbort,"Scrap handler not initialized"));
  71.  
  72.         /* find out if the scrap has a TEXT on it */
  73.         ScrapLength = GetScrap(NIL,'TEXT',&Offset); /* we can only handle text */
  74.         if (ScrapLength < 0)
  75.             {
  76.                 return NIL; /* no scrap */
  77.             }
  78.  
  79.         /* allocate a place to put the scrap */
  80.         TheHandle = NewHandle(ScrapLength);
  81.         if (TheHandle == NIL)
  82.             {
  83.                 OSErr            Error;
  84.  
  85.                 TheHandle = TempNewHandle(ScrapLength,&Error);
  86.             }
  87.         if (TheHandle == NIL)
  88.             {
  89.                 return NIL;
  90.             }
  91.  
  92.         /* save the scrap in a local heap block for them to use */
  93.         GetScrap(TheHandle,'TEXT',&Offset);
  94.         TheData = AllocPtrCanFail(GetHandleSize(TheHandle),"Scrap");
  95.         if (TheData != NIL)
  96.             {
  97.                 CopyData(*TheHandle,TheData,GetHandleSize(TheHandle));
  98.             }
  99.         DisposeHandle(TheHandle);
  100.  
  101.         return TheData;
  102.     }
  103.  
  104.  
  105. /* make a copy of the block and put the data into the scrap */
  106. /* returns True if successful */
  107. MyBoolean        SetScrapToThis(char* DataToCopy)
  108.     {
  109.         long            ScrapSize;
  110.         EXECUTE(long ErrorCode;)
  111.  
  112.         ERROR(!ScrapInitialized,PRERR(ForceAbort,"Scrap handler not initialized"));
  113.         CheckPtrExistence(DataToCopy);
  114.         ScrapSize = PtrSize(DataToCopy);
  115.         ZeroScrap();
  116.         EXECUTE(ErrorCode = ) PutScrap(ScrapSize,'TEXT',&(DataToCopy[0]));
  117.         ERROR(ErrorCode != noErr,PRERR(AllowResume,"SetScrapToThis:  non-zero error code"));
  118.         if (ScrapSize >= ScrapFlushThreshHold)
  119.             {
  120.                 UnloadScrap();
  121.             }
  122.         /* how can we tell if it failed? */
  123.         return True;
  124.     }
  125.